﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Weazel.Willows.PreliminaryModel
{
	public class Transition
		: Weazel.Willows.Model.Transition
	{
		public Transition(XmlElement xmlElement)
		{
			read(xmlElement);
		}

		private Transition()
		{
		}

		public Transition Clone()
		{
			Transition clone = new Transition();

			clone.targetStateName = this.targetStateName;

			clone.condition = this.condition != null ? this.condition.Clone() : null;

			foreach (string sfg in executionSet)
				clone.executionSet.Add(sfg);

			return clone;
		}

		private void read(XmlElement description)
		{
			Console.WriteLine("reading transition");

			// get target state
			this.targetStateName = description.Attributes["target"].Value;

			// get the execution set
			foreach (XmlElement element in description.GetElementsByTagName("SignalFlowGraphRef"))
			{
				System.Diagnostics.Trace.Assert(element.HasAttribute("name"));
				executionSet.Add(element.Attributes["name"].Value);
			}

			Console.WriteLine("Conditional: " + description["Condition"] != null);

			// get the condition (if it exists)
			if (description["Condition"] != null)
				condition = ExpressionReader.Read(description["Condition"]["Expression"]);
		}
	}
}
